//
//  STDSIAPTool.m
//  STDSVideo
//
//  Created by 2022_cgp on 2021/1/11.
//  Copyright © 2021 stdstb. All rights reserved.
//

#import "CAIAPTool.h"

@implementation CAIAPTool

#pragma mark -- Singleton
+ (instancetype)shareTool {
    static CAIAPTool *manager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        manager = [self new];
    });
    return manager;
}

- (void)ca_requestAllProductWithCompletion:(void (^)(BOOL, NSString * _Nonnull))completion{
    NSSet *set = [NSSet setWithArray:@[@"camera_month", @"camera_season", @"camera_year"]];
    CAWeakify(self);
    [[XYStore defaultStore] requestProducts:set success:^(NSArray *products, NSArray *invalidProductIdentifiers) {
        NSLog(@"code %@ invalid %@", products, invalidProductIdentifiers);
        CAStrongify(self);
        self.products = products;
        CABlock(completion, YES, @"商品请求成功");
    } failure:^(NSError *error) {
        NSLog(@"goodsError - %@", error);
        CABlock(completion, NO, @"商品请求失败");
    }];
}

- (void)ca_buyProductWithId:(NSString *)productId
                 completion:(void (^)(BOOL, NSString * _Nonnull, SKPaymentTransaction *transaction))completion{
    [[XYStore defaultStore] addPayment:productId success:^(SKPaymentTransaction *transaction) {
        CABlock(completion, YES, @"购买成功",transaction);
        [kUserModel setVipProduct:productId];
    } failure:^(SKPaymentTransaction *transaction, NSError *error) {
        if (error.code == SKErrorPaymentCancelled) {
            CABlock(completion, NO, @"已取消购买", nil);
        } else {
            if (@available(iOS 14.5, *)) {
                if (error.underlyingErrors.count > 0) {
                    NSError *err = error.underlyingErrors.firstObject;
                    if (err.code == 3532) {
                        CABlock(completion, NO, err.localizedFailureReason, nil);
                    } else {
                        CABlock(completion, NO, err.localizedDescription, nil);
                    }
                } else {
                    CABlock(completion, NO, error.localizedDescription, nil);
                }
            } else {
                CABlock(completion, NO, error.localizedDescription, nil);
            }
        }
        return;
    }];
}

- (void)ca_restoreProductWithCompletion:(void (^)(BOOL, NSString * _Nonnull))completion {
    [SVProgressHUD showWithStatus:@"正在恢复中..."];
    [[XYStore defaultStore] restoreTransactionsOnSuccess:^(NSArray *transactions) {
        if (transactions.count != 0) {
            NSTimeInterval timeStamp = 0;
            SKPaymentTransaction *transaction = nil;
            for (SKPaymentTransaction *trans in transactions) {
                if (trans.transactionState == SKPaymentTransactionStateRestored || trans.transactionState == SKPaymentTransactionStateRestored) {
                    if (timeStamp < [trans.transactionDate timeIntervalSince1970]) {
                        timeStamp = [trans.transactionDate timeIntervalSince1970];
                        transaction = trans;
                    }
                }
            }
            [SVProgressHUD dismiss];
            if (timeStamp > 0) {
                CABlock(completion, YES, @"恢复成功");
            } else {
                CABlock(completion, NO, @"没有可恢复的订单");
            }
        } else {
            CABlock(completion, NO, @"无可恢复的订单");
        }
        
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        CABlock(completion, NO, error.localizedDescription);
    }];
}

@end
